package com.siecom.transport.handler;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;

import com.google.protobuf.ByteString;
import com.siecom.constconfig.ConstDefine;
import com.siecom.framework.device.SiecomDevice;
import com.siecom.framework.device.SiecomTask;
import com.siecom.framework.module.FingerPrintModule;
import com.siecom.hostservice.MainActivity;
import com.siecom.module.DeviceCallback;
import com.siecom.module.DeviceInterface;
import com.siecom.proto.Message;
import com.siecom.tool.ByteTool;
import com.siecom.tool.ImageUnit;
import com.siecom.tool.LogUtil;
import com.siecom.transport.CommServer;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;

/**
 * Created on 2017/1/17.
 */

public class FingerPrintHandler implements ServiceHandler {
    private CommServer server;

    @Override
    public void onReceive(CommServer server, Message.siecomproto msg,String message) {
        this.server = server;
        Log.e("FingerPrint", "onReceive");
        switch (msg.getCmd()){
            case ConstDefine.FINGERFEARURE_REQUEST:
                getFeatureP350(msg);
//                getPicP350(msg); //读特征码和图形
                break;
            case ConstDefine.FINGER_VER_REQUEST:
                getVer(msg);
                break;
            case ConstDefine.FINGER_CANCEL_REQUEST:
                cancelFinger(msg);
                break;

            case ConstDefine.FINGER_PIC_REQUEST:
//                getFeatureP350(msg);
                getPicP350(msg);
                break;


            case ConstDefine.MATCH_REQUEST:

                matchFinger(msg);

                break;
            case ConstDefine.TEMPLET_REQUEST:

                getTemplet(msg);

                break;


        }

    }


    private  void matchFinger(Message.siecomproto msg){

        JSONObject json = null;
        String fea = null;
        String tmpl  = null;

        try {
            json = new JSONObject(msg.getRequest());
            fea = json.getString("feature");
            tmpl= json.getString("templet");
        } catch (JSONException e) {
            e.printStackTrace();

        }

        DeviceInterface.fingerMatch(fea.getBytes(), tmpl.getBytes(), new DeviceCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.FINGE_MODULE);
                msgBuilder.setCmd(ConstDefine.MATCH_RESPOND);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));

                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onSuccess(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.FINGE_MODULE);
                msgBuilder.setCmd(ConstDefine.MATCH_RESPOND);

                msgBuilder.setError(0);

                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        },msg.getTimeout(), false);

    }

    private  void getTemplet(Message.siecomproto msg){
        DeviceInterface.getTemplet(new DeviceCallback() {
            @Override
            public void onStart() {


            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.FINGE_MODULE);
                msgBuilder.setCmd(ConstDefine.TEMPLET_RESPOND);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));

                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
                MainActivity.stopFingerPrint();

            }

            @Override
            public void onSuccess(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.FINGE_MODULE);
                msgBuilder.setCmd(ConstDefine.TEMPLET_RESPOND);
                msgBuilder.setRequest(String.valueOf(bundle.getInt("type")));
                msgBuilder.setRespond(bundle.getString("data"));
                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onCompleted(Bundle bundle) {

//                MainActivity.stopFingerPrint();
            }

        }, msg.getTimeout(), false);

    }
    private static final String fingerBmpStr = "89504E470D0A1A0A0000000D4948445200000098000000C80806000000144C28D60000000473424954080808087C0864880000141749444154789CED5DDB76E4380E73CDE9FFFF65EFCB38AB56780148D055C9184FDD2589A4258A17B892BCCEF33C8F070F86F0CFBB0D78F0BBF138D883513C0EF660148F833D18C5E3600F46F138D883513C0EF660148F833D18C5E3600F46F138D883513C0EF660148F833D18C5E3600F46F138D883513C0EF660148F833D18C5E3600E5EAFD7BB4DF815781CCCC0EBF53A9E2FFA6AF038D8822B6A3DCEA5C37FDEC15EAFD7E35883F8F36E03DE892715CEE33FE9604FC4BA0FFFA914B93AD6E35CF7E0D73BD85363BD17BFD6C12EC79A8C563B5776FDFFD25DE5D27E1307F7EB6AB0F570D48E654542CFC92CFD56533169EF27E0D738587650D534B9D76D6C74891CD0B3C772E09FEA7CAF9FFEBB2998888144A07DCC8B38D7E796FE754EE490DED67B6B7EE251FDC808864491688EE538AB73B0075FB1C1725044DE4F73B21F55E4EF85B3175DD0435BE759A90CADB52C5B22549C8BD5F129F831112CABB1AE74E41D5AB598AE38AB853D5DA2722327F7E67D123ECEC1A2BAC91A8B3636AA81B2DACB9A5341D47078F3AD71CBDE9F90323FC6C1B24DF51C8FEDB290FAAAD22D2AB0DA86A4D14F77AEE3788383750B746FBCF35915534E983917EA7C9F10E1C61D8CD924A4DE406A1D6B9EA7CF4B3D99BCCC86AC29C8D071AE75BCD38C2830EA60190FC54418346DA11BCA6E349336AFE7CE5E1779172A73F04EE4BC3BAA8D3A58C652EFB036AEDAD223F2A752687680DE3365AF92D639D9658DE4DCE964E34C7EC69E479F770FDB4B8113FAD69AA79AC6195DD7FA77D75819C62218DBDD5536BBEA14554ECCB3C1931DCD573857A6D78A7477E323BBC86B333C0762BAA829A0C4A9CAA13C64AFC4B279D38E37E2604C0788A6B07D3C6B06902E32B30339149691472F45D4892A9D62BACB943B185A5FED9F459F5BF22AE3AAD4588D46DD74DED58FEA533A99D4C194B50DAA27FB9C8D5CFBE76C8DB5EB669D01A137A6A174B29114897CDFAA7280AA08A084D5D1DDE51C1929CC74D13B54E9B8ED60EC8D8ED63011A8739895C812D9B6FF3B5AD3B523D217D96545A58ACD2CA4112C8A46D59BA07EE0EC50F70361BBC3C9DAAED3515EE37B04439C2E9A9F619C07EBCCCF9C815D83DEFA487E26070113B9D451A64B26B368391812A1B2A896D10D88EECC866CDDF5399B76BB053F13B1A6EABA697EACEC60DD22BDFA0055F21559D73D7024FDA21DE2BB5F01A9527FC9C118B2325AC33843B5E699E8EC58CAE31A430EA9438BA0A8EC49B5AB6CA5C86A4AEA16ABD65C2F7AA0CC7934A76297055457A533DFF5647490F759A69F7534FAA78AD0D4C83AD179DA3FE2BF7ECE46C8C9572A28BCE7B2C699C23F93BBA3EBB4963C44261CC15469F14EBA4211BDBA3232F93B100A659DEB3967259AAF3255A99A4E91084BCF8C656B2A053D02A47BEAA644C401AB69F3FABFAA465BE56465C7FA799632530753747E9EBCE8F38A2EA49344C6A339EBDC4E74AB14FC885D4A30CD8CD7C4C011AC9AEF3B85EB44F45245AE8A7E9412A914DFAC1D7BE4E93C4F84D0C1D86E024D71E878C7AEAAAE4EF4B3D2CCBE3E035BFB58FB8FBCCDB05E11ED510849EF6933773A33D86E11FD7C1D9FAAAFEE04DBD4A86B3FD471216730E6746D3123189B7B3D45C86632FA3F1115CE4E05CF19A2C6208BE4196FC692C9660443D31FF339B28E75D60E4D8044CA2E0D81EA61ED7967F1CF46E46F0E86147FD508A4A400BA40EA94491B2BE4AA8577ED63E6E4D7F89F7D30739E2A55A13AB4698214797EE41914D1CFB347D94D56E55A1968EF92CFF33C5EC7719CFB444FD9CEF622CA3BC5EB64CD32D160A8D86F4B5657DE2A330B264ACAC2743074F33B066651A4923A506EA9DA9830DC55A7665495125910A8729B8CEE2F07CB1C2B53C2DE38750AA916D1DDC3649E1B4DEF88AC7D3E5AE264F6EDE3ED2072240EC60239301507C61E184B04A3365CB22D794CE4649EFF6EAAA79AB2BFBA4815519A19A9E830D9A8E5CDED340328D49D335AFB4E03DD9B3F959471CDA984FC0C2AE7CAE44D75CB537827F76501E14A8F6363F2A7792A8441F6A08CA88893AA229B32027664AE367BE75C39FF55A669EBB1D4609170754ABC63BD27F338FA05BD3AB566057675EFD15A6D8AB2F8F62EB2BB7177B4D88C3D96CC6E14543ABDAAA6B2E4B034042283D99FF33C8F3FC8A6A3C2559CD4BB90D9A264F0BBA9AF5223A2CFE7A5D37D0CD1F78D68DD8565C655791B6F7E97B06429902E7B8FDAB5EBBB504DA5D6FA3B2EAE977A3D7DE6D775AA862A98EE4C967773BB85BB2773E2F9A70EFF8E6C70E9D8239CF77CE56FB4766E7EC5B9AA0714D939D14478FA2F54D3E0BAAE6AF3FEBC5937C9D6A3BBB3B9291221202D0595B98A945901FA8C7712AD935C5795D7EBA670EA1BADCC6177BA8FEAB8A51F716A740C91EDC9AAD466AC5344C5B9876C7FD873DCF5FF4137A1320F31289A87BC5AAA3A50259A32A838E13EBFD28D7AF22A11D94B7F4CB0F8F685C31DD5A8856E2C5BCC676BBB63914D2B14E9B5DA8147D1CD93C15E967D7E54D4877A8F80A65805751DC69AEBCDEF38091BF1D4DD1DD33854688D1DDD28DBA16D10DBC22E92D90055015BA51B32FAA222B3822CB229BAC1CABA2CFA5508D5AC213BCF33E6C126D240B406294CAB37AE4A5330CFC9D4A6E83C6517CBD8D1A19F561DDF1C8CE9A2D8791587AD16F38C9D9DF555CEA82B8BB5CBE2C0F6689AD5BD6CC3769EDB0F7D20E964A226AB006D1226239742CE44E7DA91B5FB00DB4CECB2DC9F8B8C944FD0148C2EE4D03A174449B24E5DC4C9E2BFAAD3D2FD978375B921B62363C22E8AAE6346E34A746A580B775C066F5D644FCA83598BBAF32AB551D73954CED54D9D1D5A445DF0EF29B0E264E93E1C010FC676466A621101CAA955C7ADF95DE7BA2342AEFA18EC4EE73509B0BC23215A23308753B9B9DE43A1B2EE6C5A3A8EA746E45828D1DC29EE57947E8D79D5B190F9D79AD7CBFE2D80ACA3AABA4054DFDD609C8999A3AAB5A908A6E0B522990A6765A256A6531901331DAA2EB24B7330D905D1FFE5604CBD35E5341532D50BEB9FC0E34DA54D55545DF577655AE7F47ABDFC9FECAE424D6B649D0FA26797A5E4FADE5557A93A4ACBC1143ABEE41F8D22DF12781CBA56DF9BC36C067A30CAB419A113B59875D5DA37C2ED5DE471F0A9441121266A28A5BC773403133599279F91693A9872932AD1464152AA6BAD773817DBE546B6A9E0E9F36CA5BE93AF320699ABD0891C0E751B6FE4B9B2FDB89BB4BD10F168D679CB6A304FA96758652EA3F7138AF43B9CAE4B6F7845BEA2C03F0EA006636E4AE7D591226DAA74227A334C3957A7D067A0B0FD3C9D6FB4AA1579CA3DF9E5825210B5540E38B16F488A5EED523863E74DC1EB25FC4E3EAA34938BDCBAA9575595827F1AEA08EE212BD63DDDD257451DA85EE1B0D1ADF2A6E013EAB79F82943B3B4807531FF0946E35C1CACEFD54DCC5D37D63F2A75E91A8B82D7463D4C4E93EF79D8E75E77BD44B4E5706FCAB03983968A159495F882C4F9E67232AF79D4EC8EC85A2F3BDF4756B3C294DB11BA4AAB5BC79D5879F7863C0DAC062AAD4E822B3C5ED223B8E15AD5317F2EFB0AF63078335A228C8E70B952E714F975E84DBF5B8EF2251633E2585643A2F4C92AB4A28885E0BEA67A3BBC86A97A88E081D745E552939A0BB3AB64BD78E775F94F3347EB2BB4B94EEF3567478A84CA7E5E82C51ACB850E81C05EEA8FFBA0122FDC1DBAA325597361101278BF8EC15982AE5DD9DEA76A0FAE11FBC55D3196824507063965E1613AFC0A65EC565B2F67796DE679EDDDEE7966D529A42C92D2187D575AE3BD8FD7D5C4D2C237621FA947AFED279040EC63C24E35CDD3451D581D67117D4CF32DD10BDB3D0BFF662B721E5C110C1C7817769A8BC4B261B4DBCB7078C73551A18745EB7BC4823C69B3A478BAFFBD645AE03FBE42A989460A11B71D80B3059F3A9529EEA5C7639E8A561F4D3DFC9AFD464D1FC8C0956D891C988F4559B0286CE88E6758A7C4F3EC2C257E45A3089D66E8DA0E4C9BC35DD5B3CC955296AC50BAA5ACC82FA2D812513FE7B91DD54E7CD63EA2515717A0759CAD20FFB676AC7525EA4E8527C0B1A8788A6A81C08728357A8E62A8BFD0AAA6F1DF6F51EDE55E8AFB86C94FED007EB8469FE36DA5ED6067511DF8D6CDEFA6A173EC1E85BA42BFBDC5F677C2411AC8B6AFBDF3DE84952D52A98D545BA92C167F4B0C8EC2AFD02BA092007E5BDCEB0E45C606F5C6457B5ADF7E47738BF0C48E44388596F8E5B73ED91F9188A606C24FA32A899E2B2792C3581D67D96DDCAB706A89C084CE455E9868BFCAE3115D9EC0DBBABC3CCA0A07AB2B508DE55ECFFF58CE7BFFF538464B4C652726419DDB163D2B122B999FE6C9D874927DACFAAF226A3952259852C4D10CD7D47915F41F5CDC76ED3C405A816F814B370141CECAE87B56A86688E35AFC3942B08DF6E0DAAC26A4FA56E5B9F992174C7698A0C55C789E64C16D768E4ECF26D2A4CF268886CD8C1549BA2A849322AA3BA3E93A1B02FD35FD187A27299DA7B1115F913F5895A66579EB7BED28C208DC6141DA1AAD598576F08C208A60CE5D507AFA4A429A7B3E67579396B6E04AB26EAA05AFB5A73CDF1A35883558A58264A766A994FE80C3FADC847F457188175AD25ABD5456646559D6B1D8BD675D24097E454458FE9AE11C5D4ABA9119AA25BF0561C4F1551A3F9D91A257D81DA8474DB3BBC343BF1FA484E5330CEE5CDC99C681D9B4C318A6761E7797351C7517394DDA29FFA9BDD8831DD1B8C3AD79463A9E54E14FA2BDE59B321FAA95F1D9029EE16DC5947B8CB51D730E8E631E9B8EA547730FAD6FF5939165659698AACD64A96119582BD1251984D43DE103072956915017B060C3A5DE5978CA3518321CCB9AACBCC6455C016F0D19C4EC7CCD87837BA3CE2C8375A914DA946AE29A7AB5E94EEB8373F4225EDDEC9ECAF63ADBFD99D1678C5F18C7CEDA24BA364E39568A58C70CA48EFED3FEA78529A42F98AC692D1D94CB68047E6749CAB525CA385F55D409C4CE660E8C17423C60A84F7B9CBB198F14C4FB4065D57916BA11D4D0FD0C1101A8235685F8F7641563D8632DAD9BC8EF3B0F6475047B82EAAE79A7E275FCD0CA3C81C24720C25BB7E572D56BDA88A4BC6C867FDE14FB750478C52C09333695F25A557A80F747EB4DEB3A3BBFFDDC013A6C86C83A7A21E9236A76BA98AFC6992759A176480361C214DC114DCEBE753AC7FA43BD3C1BC55C8E477DF48A83BDF2A3ACE1EEDEF7A414B5D6474C3D9355D363D4B33EC78B5885739D6DD6F2F3274A3EC1F6B7022F42A6422323A3565654CD54430723354BA492F22B175E2DE84515FD7412397A288DEE578B23A6971AA8E5476B18C2E0BAA6051BD08D4AB228573A1B76BD2B9BAD44227DD779DEA5D85BE470F6511AEF5B27BDF68557E679CB4EA78950E506513A2632275A23A225DC899AD7B9C16F94CAAE87671C83CB573A1F21497019583CC47C15C7A55C45DD748BF4D811AD0E1A790716B4EC7B9105489CE2ED98ACAF4C0446BF4B2ACE9B44453ACCA58AFEE46B66CFD3E2773BA4E1DE68D57BA478553BD5EB5BF8C52D5B7C275B6C37030A4BBDB1F2652821A3A556F59E3D633769C1519CF6C623041295564666B681E6CE5477627AB4435D506B1C53CCA6B7523AC25E753BB47B4D3CEEC58CFF52B82B1B74FD1297580A42864ACDB987C7291AF3C8F6A302815F9D1EDDF23DA5D3CCDAAF3B285194323DA2EAF72C32D9B98358C2C74BCFB1C1EA83F258328D9C32CBB814C9A65A913CB3E36A2A0363229339395E9B0B047688F285D65AD014256BA9CFF4AACD616BB81DEFA6F8A459C4B668327B352A329CA08744EB6CE82C229D4654EFB0B870CE782CA513A17932AAD6761361C8D6C15C2D2C264A1BF36731D5DE37FE9C30BCDEAA2362204ADCF2B355745E63ECE5E589513B1F5B02C451E0ED13A5DA0778B5B240AB163CC7C74BC1AAD14C46B06358766C93323586563AAFC1122AB621FD3295A636AA6DE43B73460F521BA98B498A6F16389604AA214EDA2B2EE46C92A33409A964EA352B5F5EE42BF532FBE5EAFFF47B009FE8A89323B8786228B945924626AB16AFAEDCEF33059C22074D55F8CBD51039FE7F9FD1BADDE6465B1E9E98AE679F3D9BA6A5F873A244AC37479AFA9227F02C8FE4ABB4825B9E845B48AE32176B27C59B426B2434561443232C83AC497FFED8DEBDF29939FA516CF0914453FE2342CDD10398C22027AF6AA49576BFD27E2CBC154E45F36BF7AD3196E8B2558197D917C54172307B529D33585EC527E2BF2D11BE96D4EB72B62A3D66E93C23E342D46EB151D64A4EBD36BB3E3D8BAC87DF09AC0D007156E8A1947E75CE814CC510950753CC40E5591DF25B251F95649B5EA0E8BFC3B8CAC1C7AF659944EBB4EACA82D3DDB3A32F6F5FB05B933E2ADBAD3DF4D817463C878A5CBDB6DB1E6794EC7D46C28AADC57848E0C649FD45DE9CE7B45F69B29D2037B73AB754C9727DB9175BB99AE6A5A47299B4FEF1CF77A77BFD4EBFE5A17DE245A3D052B32A2325A8F9296D17A6FBCCB9423A9052DE22DB977638A288FFCC2EC222B4AB2CFD56931731EC5CDEE12959FC6C4ABF55FE507529EA42992891EAA2ECA9BC3D47B998E4A6DA868107E0BB2DA7C0515C1B23A26223AD99AA933A7937EAB7677A1784DA44487285E1D30A5293C054C2B1F8D31690F711C96A8FD147C924D1679EDF15D59AD0A47B0ECA0BDE8A58A00AAF474F7413244F534D006C83A3FB3435C2295E768A9834511827526B6E8EF92B0C886A217A76B5F24EB2E543AEE9D8688E6EE328FA3F1751DE46632C534938A27BA49B5B32878AE6987DC235016D5ACB911CF781CE2EF832989CAE3A84509B69B64792EC4A6AA8374CB898A5E3420643A3DD00EA6783D82A29AF6BAF2A3B48FA407C6AE6A1382D676CC7E213555A67FC7D8CF456651A0B256C1B5651B371931BD35C8BA2A81CB3A9CBA411B73B009323583EAD679F33BE9DE925989C0AAAC819E4FF74CDABF0478370899AF28CCEF2244154E751CFDC25D598E74EB2E4AD739743268D7C8A40BF40D41B586FA2DF0DEAEAC9F65359CCA2DC66B3055DA43C1D41055E763F8A0AA435BEB2B7B99BDDE63C62A188B6093E8386E76600A0AA2F35EF1071E4788F1DFAE3301C521783290B713DD26E2B73951841F19C1DE8D8977ADBF153F3282BD1B8F53E1F8E7DD063CF8DD781CECC128FE076411BD77A6BFEF510000000049454E44AE426082";

    private void getPicP350(Message.siecomproto msg){
        SiecomDevice.ReadFingerbitmap(new SiecomTask.TaskCallback() {
            @Override
            public void onStart(Bundle bundle) {
                MainActivity.startFingerPrint();
                MainActivity.getmSys().DLL_SysLedControl((byte) 10,(byte)1);
            }

            @Override
            public void onSucceed(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.FINGE_MODULE);
                msgBuilder.setCmd(ConstDefine.FINGER_PIC_RESPOND);

                JSONObject jb = new JSONObject();
                try {
                    jb.put("code",0);
                    jb.put("factory",FingerPrintModule.FINGER_TYPE_STR);
                    jb.put("picture","");
                    jb.put("fpld",bundle.getString("print"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                msgBuilder.setRespond(jb.toString());
                msgBuilder.setRequest(String.valueOf(bundle.getInt("type")));
//                Bitmap bitmap = bundle.getParcelable("Img");
//                LogUtil.e("TAG","finger Bitmap counts == " + bitmap.getByteCount());
//                LogUtil.e(TAG,"finger Bitmap == " + ByteTool.byte2hex(DeviceInterface.Bitmap2Bytes(bitmap)));
//                ByteString bitmapByte = ByteString.copyFrom(DeviceInterface.Bitmap2Bytes(bitmap));
                //-----压缩图片---------------------------------------------------------------
                byte[] bytes = ByteTool.hexStr2Bytes(fingerBmpStr);
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                bitmap = ImageUnit.matrixCompress(bitmap,0.5f);
                ByteArrayOutputStream output = new ByteArrayOutputStream();//初始化一个流对象
                bitmap.compress(Bitmap.CompressFormat.JPEG, 1, output);
                bitmap.recycle();//自由选择是否进行回收
                byte[] result = output.toByteArray();//转换成功了

                //----------------------------------------------------------------------
                ByteString bitmapByte = ByteString.copyFrom(result);
                msgBuilder.setExtion(bitmapByte);

                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
                MainActivity.stopFingerPrint();
                MainActivity.playVoice(ConstDefine.OPERATE_SUCC);
                MainActivity.getmSys().DLL_SysLedControl((byte) 10,(byte)0);
            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.FINGE_MODULE);
                msgBuilder.setCmd(ConstDefine.FINGER_PIC_RESPOND);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));

                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
                MainActivity.stopFingerPrint();
                MainActivity.getmSys().DLL_SysLedControl((byte) 10,(byte)0);
            }
        },msg.getTimeout());
    }


    public static Bitmap byteArray2Bitmap(byte [] data) {
//        //不应该这样设置
//        Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
//        bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(data));
//        return bitmap;

        return BitmapFactory.decodeByteArray(data, 0, data.length);
    }

    public static byte [] bitmap2ByteArray(Bitmap bitmap) {
//        //一开始这样写，这是错的
//        ByteBuffer buf = ByteBuffer.allocate(bitmap.getByteCount());
//        bitmap.copyPixelsToBuffer(buf);
//        return buf.array();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    private  void cancelFinger(Message.siecomproto msg){
        Log.e("cancelFinger","~~~~~~");
        MainActivity.stopFingerPrint();
//        DeviceInterface.cancelFingerPrint();
        SiecomDevice.cancelLastTask(false);
        MainActivity.playVoice(ConstDefine.OPERATE_FAIL);
//        DeviceInterface.fingerCancel();//取消读指纹
        com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
        msgBuilder.setModule(msg.getModule());
        msgBuilder.setMsgid(msg.getMsgid());
        msgBuilder.setCmd(msg.getCmd() + 1);
        msgBuilder.setRespond("Finger cancel");
        msgBuilder.setError(0);
        com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
        server.sendToRemote(respondMsg);
        MainActivity.getmSys().DLL_SysLedControl((byte) 10,(byte)0);
    }


    private  void getVer(Message.siecomproto msg){

        DeviceInterface.getFingerVer(new DeviceCallback() {
            @Override
            public void onStart() {


            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.FINGE_MODULE);
                msgBuilder.setCmd(ConstDefine.FINGER_RESPOND);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
                MainActivity.stopFingerPrint();
            }

            @Override
            public void onSuccess(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.FINGE_MODULE);
                msgBuilder.setCmd(ConstDefine.FINGER_RESPOND);
                msgBuilder.setRespond(bundle.getString("data"));
                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onCompleted(Bundle bundle) {


            }

        },msg.getTimeout(),false);

    }
    private static final String TAG = FingerPrintHandler.class.getSimpleName();
    private void getFeatureP350(Message.siecomproto msg){
        SiecomDevice.ReadFinger(0, new SiecomTask.TaskCallback() {
            @Override
            public void onStart(Bundle bundle) {
                MainActivity.startFingerPrint();
                MainActivity.getmSys().DLL_SysLedControl((byte) 10,(byte)1);
            }

            @Override
            public void onSucceed(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.FINGE_MODULE);
                msgBuilder.setCmd(ConstDefine.FINGERFEARURE_RESPOND);
                JSONObject jb = new JSONObject();
                try {
                    jb.put("code",0);
                    jb.put("factory", FingerPrintModule.FINGER_TYPE_STR);
                    jb.put("picture","");
                    jb.put("fpld",bundle.getString("data"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                LogUtil.e(TAG,"fingerInfo == " + jb.toString());
                msgBuilder.setRespond(jb.toString());
                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
                MainActivity.stopFingerPrint();
                MainActivity.playVoice(ConstDefine.OPERATE_SUCC);
                MainActivity.getmSys().DLL_SysLedControl((byte) 10,(byte)0);
            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.FINGE_MODULE);
                msgBuilder.setCmd(ConstDefine.FINGERFEARURE_RESPOND);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
                MainActivity.stopFingerPrint();
                MainActivity.playVoice(ConstDefine.OPERATE_FAIL);
                MainActivity.getmSys().DLL_SysLedControl((byte) 10,(byte)0);
            }
        },msg.getTimeout());
    }



}
